package com.leigang.java.code.design.mode.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 具体的Subject实现
 */
public class WeatherStation implements Subject{
    private final List<Observer> observers = new ArrayList<>();
    private String weather;

    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(weather);
        }
    }

    /**
     * 天气更新时调用此方法
     */
    public void setWeather(String weather) {
        this.weather = weather;
        notifyObservers();
    }
}
